package net.satiri.panaroekan.activity;

import net.satiri.panaroekan.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PanaroekanConfig extends Activity {
	
	private EditText txtPin = null;
	private EditText txtServer = null;
	private EditText txtTimer = null;
	
	private Button btnSave = null;
	private Button btnCancel = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.config);
	    
	    initForm();
	    setBinding();
	 }
	
	 private void initForm(){
		 btnCancel = (Button)findViewById(R.id.CmdConfigCancel);
		 btnSave = (Button)findViewById(R.id.CmdConfigSave);
		 
		 txtPin = (EditText)findViewById(R.id.TxtPIN);
		 txtServer = (EditText)findViewById(R.id.TxtServer);
		 txtTimer = (EditText)findViewById(R.id.TxtTimer);
		 
	     SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
	     String panaroekan_server = sharedPreferences.getString("panaroekan_server", "");
	     String panaroekan_pin = sharedPreferences.getString("panaroekan_pin", "");
	     String panaroekan_timer = sharedPreferences.getString("panaroekan_timer", "");		 
	     
	     txtPin.setText(panaroekan_pin);
	     txtServer.setText(panaroekan_timer);
	     txtTimer.setText(panaroekan_server);
	 }
	 
    private String getValue(EditText text){
    	return text.getText().toString();
    }
	    
	 private void setBinding(){

		btnSave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				SavePreferences("panaroekan_server", getValue(txtServer));
				SavePreferences("panaroekan_pin",getValue(txtPin));
				SavePreferences("panaroekan_timer",getValue(txtTimer));
				
				finish();
			}
		});
		 
	    btnCancel.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});	    		 
	 }
	 
	 private void SavePreferences(String key, String value){
		 SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
		 SharedPreferences.Editor editor = sharedPreferences.edit();
		 editor.putString(key, value);
		 editor.commit();
	 }
	
	
}
